// MainWindow.cpp
//
// Copyright (c) 2008 Bojan Rajkovic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//

#include "../include/MainWindow.h"
using namespace LinPulseApplet;

MainWindow::MainWindow(int *argc, char **argv) {
	
	kit = new Gtk::Main(argc, &argv);
	gladeXML = Gnome::Glade::Xml::create("linpulse-applet.glade");
  	
	gladeXML->get_widget("linpulse-applet", mainWindow);  		
  
	if (!mainWindow) {
		throw LinpulseAppletException(_("Could not instantiate main window."));
	}
	
	gladeXML->get_widget("keysCount", this->keysCount);
	gladeXML->get_widget("sessionKeysCount", this->sessionKeysCount);
	gladeXML->get_widget("runtimeAverage", this->runtimeAverage);
	gladeXML->get_widget("uptimeAverage", this->uptimeAverage);
	gladeXML->get_widget("lastPulse", this->lastPulse);
	gladeXML->get_widget("pulseButton", this->pulseButton);
	gladeXML->get_widget("terminateButton", this->terminateButton);
	gladeXML->get_widget("connectButton", this->connectButton);
	gladeXML->get_widget("disconnectButton", this->disconnectButton);
	
	pulseButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Pulse));
	terminateButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Terminate));
	connectButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Connect));
	disconnectButton->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::Disconnect));
	
	updateGUITimeout = Glib::signal_timeout().connect(sigc::mem_fun(*this, &MainWindow::UpdateGUI), 100);	
	
	daemonTalker = new UNIXClientSocket("/var/lib/linpulse/ctrl");
	this->connected = true;

}

void MainWindow::Connect() {
	if (!this->connected) { // it's been delete-ed
		daemonTalker = new UNIXClientSocket("/var/lib/linpulse/ctrl");
		connectButton->set_sensitive(false);
		terminateButton->set_sensitive(true);
		pulseButton->set_sensitive(true);
		disconnectButton->set_sensitive(true);
		this->connected = true;
	}
}

void MainWindow::Disconnect() {
	if (this->connected) {
		delete daemonTalker;
		disconnectButton->set_sensitive(false);
		pulseButton->set_sensitive(false);
		terminateButton->set_sensitive(false);
		connectButton->set_sensitive(true);
		this->connected = false;
	}
}

void MainWindow::Terminate() {
	if (this->connected) {
		daemonTalker->Write("do term\n");
		delete daemonTalker;
		Gtk::MessageDialog deathWarning(_("Warning, will close applet now since daemon has been shut down."), false, 
			Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
		deathWarning.run();
		Utility::LogMessage(_("Going down, we terminated the daemon from the applet."), M_INFO);
		exit(EXIT_SUCCESS);
	}
}

void MainWindow::Pulse() {
	if (this->connected) {
		daemonTalker->Write("send pulse\n");
		daemonTalker->ReadLine();
	}
}			

MainWindow::~MainWindow() {
	delete mainWindow;
	delete kit;
	delete daemonTalker;
	delete keysCount;
	delete sessionKeysCount;
	delete runtimeAverage;
	delete uptimeAverage;
	delete lastPulse;
	delete pulseButton;
	delete terminateButton;
	delete connectButton;
	delete disconnectButton;
	updateGUITimeout.disconnect();
}

void MainWindow::Run() {
    kit->run(*mainWindow);
}

bool MainWindow::UpdateGUI() {
	if (this->connected) {
		daemonTalker->Write("get keys\n");
		keysCount->set_text(daemonTalker->ReadLine());
		daemonTalker->Write("get sessionKeys\n");
		sessionKeysCount->set_text(daemonTalker->ReadLine());
		daemonTalker->Write("get runtimeAverage\n");
		runtimeAverage->set_text(daemonTalker->ReadLine());
		daemonTalker->Write("get uptimeAverage\n");
		uptimeAverage->set_text(daemonTalker->ReadLine());
		daemonTalker->Write("get lastPulse\n");
		lastPulse->set_text(daemonTalker->ReadLine());
	}
	return true;
}
